Skip to content

Conversation

@GunBros
Copy link

@GunBros GunBros commented Dec 18, 2025

안녕하세요 재성님. 4단계 미션 구현입니다! 다음과 같은 질문이 있습니다!!

1. LottoMachine에 대한 책임
LottoMachine 컨트롤러는 로또 생성에 관련한 책임을 맡고 있는데 현재 수동 번호 입력 받기와 자동 번호 생성이 분리되어 있는 상태입니다. 제 생각에는 컨트롤러에서는 생성될 때부터 수동 번호들도 생성자로 받는 것이 알맞아 보이는데 방법이 잘 떠오르지 않네요.. 좋은 방법이 있을까요?

2. 자동 개수와 수동 개수
자동 개수는 전체 로또 개수에서 수동 개수를 빼서 구하는 작업을 필요할 때마다 수행하고 있는데 이 방법보다는 전체 로또 개수, 자동 개수, 수동 개수를 각각 가지고 있는 것이 나을까요?

3. 예외 처리
로또 프로그램에서 예외가 발생할 수 있는 케이스가 정말 많은데 이 예외들을 적절하게 처리할 방법이 떠오르지 않습니다.. 사용자에게 알려야하니 Application 단에서 모든 에러를 받아서 처리해야할지 아니면 각각의 책임이 있는 domain 객체, 컨트롤러 , 파서 등 각각의 객체에서 받아서 처리해야할지 고민이 되는데 조언 부탁드립니다.

Copy link
Contributor

@javajigi javajigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확실히 3단계까지 객체 설계를 잘해서인지 4단계에 변화가 크지 않네요. 👍
캐싱에 대한 피드백도 잘 반영했고요.

로또 생성과 관련해 질문과 연결된 피드백 남겼으니 한번 도전해 보면 좋겠습니다.

  1. LottoMachine에 대한 책임

  2. 자동 개수와 수동 개수
    위 2개 질문은 피드백 남긴 내용을 통해 고민해 보면 좋겠습니다.

  3. 예외 처리
    기본적으로 도메인 객체에서는 throw하고, throw한 예외를 Controller 또는 View에서 catch하는 방식으로 접근해 볼 것을 추천합니다.

import java.util.ArrayList;
import java.util.List;

public class LottoMachine {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3단계에서 4단계로 요구사항이 변경될 때 로또를 생성하는 부분의 요구사항만 변경됐다.
로또를 생성하는 부분을 다음과 같은 구조의 인터페이스로 분리해 보는 연습을 해보면 어떨까?
이와 같이 인터페이스로 구현했을 때의 잇점에 대해 고민해 보는 시간을 가져본다.
Lottos는 사용자가 구매한 n장의 로또를 추상화한 객체임

public interface LottosGenerator {
    Lottos generate();
}
  1. 자동 개수와 수동 개수
    자동 개수는 전체 로또 개수에서 수동 개수를 빼서 구하는 작업을 필요할 때마다 수행하고 있는데 이 방법보다는 전체 로또 개수, 자동 개수, 수동 개수를 각각 가지고 있는 것이 나을까요?

이 구조로 리팩터링하면서 위 질문에 대한 답도 스스로 찾아볼 것을 추천해 본다.

public String count() {
return String.valueOf(price.count());
public String toCountString() {
return String.format("수동으로 %s장, 자동으로 %s장을 구매했습니다.", manualCount.toString(), totalCount.diff(manualCount).toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 값을 출력하기 위해 이 객체의 인스턴스 변수로 totalCount, manualCount를 필드로 가지는 것은 좋은 선택으로 보이지 않음.
UI에 출력하기 위해 문자열을 생성하는 책임을 View 객체가 담당하도록 구현하는 것은 어떨까?

}

public LottoNumber(int number) {
public static LottoNumber get(int value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@javajigi javajigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인터페이스 추가하고, 인터페이스 기반으로 구현 👍
단, 이렇게 구현하더라도 로또 생성에 대한 부분의 복잡도가 main() 메서드에 드러나고 있는 것 같아요.
추후 수동/자동 로또를 생성해야 하는 로직을 다른 곳에서도 구현해야 한다면 또 다른 중복이 전파될 가능성이 높아 보여요.
이런 단점을 보완하기 위한 피드백 남겨 봅니다.
디자인 패턴 중 하나를 경험해 봤으면 하는 바람으로 피드백 남겼으니 한번 적용하면서 어떤 느낌인지 경험해 보면 좋겠습니다.


import lotto.domain.LottoCount;

public class LottoCountController {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Controller의 역할은 View와 Model(도메인 객체)를 연결하는 역할이 주이다.
그런데 이 객체의 역할을 보면 Controller 역할은 보이지 않는다.
객체 이름에서 Controller 이름을 빼고, 이 객체의 역할을 LottoCount가 담당하도록 구현하는 것은 어떨까?
LottoCountController라는 이름을 부여하는 순간 LottoCount 객체를 수동적인 존재로 바라보는 느낌이 듦


public class LottoMachine {
private final LottoPrice lottoPrice;
public class LottoMachine implements LottoGenerator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ManualLottoMachine 이름을 부여했다면 이 객체의 이름은 AutoLottoMachine으로 부여하는 것이 좋지 않을까?

Comment on lines 19 to 24
LottoCountController lottoCountController = new LottoCountController(purchaseAmount, manualLottoCount);
LottoCount manualCount = lottoCountController.getManualCount();
LottoCount autoCount = lottoCountController.getAutoCount();

ResultView.printLotto(lotto);
LottoMachine lottoMachine = new LottoMachine(autoCount);
ManualLottoMachine manualLottoMachine = new ManualLottoMachine(manualCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자동과 수동 로또를 생성하는 부분의 로직이 Controller라 할 수 있는 main() 메서드에 집중되어 있는 느낌이 든다.
자동과 수동 로또 수를 결정하고, 각각의 로또를 생성한 조합하는 역할을 다른 객체를 추가해 위임할 수도 있다.

이와 같이 인터페이스를 구현하고, 구현체가 서로 연결되어 있는 경우 아래와 같은 구현체를 통해 구현체를 통합할 수 있음.
보통 디자인 패턴에서 컴포지트 패턴으로 알려져 있음.
아래와 같은 객체 추가에 따른 효과를 느껴 봤으면 하는 바람으로 피드백 남겨봄.

public class LottosBundleGenerator implements LottosGenerator {
    private final List<LottosGenerator> lottosGenerators;

    public LottosBundleGenerator(Money money, List<String> manualLottoText) {
        this(toLottosGenerators(money, manualLottoText));
    }

    private static List<LottosGenerator> toLottosGenerators(Money money, List<String> manualLottoText) {
        // manualLottoText 활용해 ManualLottosGenerator 생성
        // money에서 수동으로 구매한 로또 수 만큼 차감한 후 AutoLottosGenerator 생성
    }

    public LottosBundleGenerator(List<LottosGenerator> lottosGenerators) {
        this.lottosGenerators = lottosGenerators;
    }

    @Override
    public Lottos generate() {
        // List<LottosGenerator> 반복문 돌며 로또 생성
        return  로또를 합쳐서 반환;
    }
}

@GunBros
Copy link
Author

GunBros commented Dec 22, 2025

주신 피드백 반영 완료했습니다!
궁금한 점이 추가로 있는데 현재 자동으로 생성할 로또 개수를 계산하는 로직이 ResultView.printLotto의 인자를 위해서 한번, LottosBundleGenerator의 toLottosGenerators 에서 중복으로 이루어지고 있는데 이걸 줄일 수 있는 방법이 있을까요?

Copy link
Contributor

@javajigi javajigi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

피드백 반영 잘 했네요. 👍
피드백 하나 남기기는 했는데요.
그리 중요한 것은 아니라 로또 미션 이 정도로 마무리하는 것으로 할께요.

Q1. 궁금한 점이 추가로 있는데 현재 자동으로 생성할 로또 개수를 계산하는 로직이 ResultView.printLotto의 인자를 위해서 한번, LottosBundleGenerator의 toLottosGenerators 에서 중복으로 이루어지고 있는데 이걸 줄일 수 있는 방법이 있을까요?
A1. 중복을 제거한다면 LottoGenerator에 자동/수동 값을 반환하는 메서드를 추가해 해결할 수는 있을 것 같아요.

public class LottosBundleGenerator implements LottoGenerator {
private final List<LottoGenerator> lottosGenerators;

public LottosBundleGenerator(LottoPrice price, LottoCount manualCount, List<String> manualLottoText) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public LottosBundleGenerator(LottoPrice price, LottoCount manualCount, List<String> manualLottoText) {
public LottosBundleGenerator(LottoPrice price, List<String> manualLottoText) {

manualLottoText 크기가 manualCount인데 manualCount는 필요없는 값이지 않을까?

@javajigi javajigi merged commit e0db869 into next-step:gunbros Dec 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants